<?php

namespace app\common\Model;


use page\PageParam;
use service\DataokeService;

class Coupons extends CommonModel{

    /*
     *      * id: 23243289,
     * goodsId: "43708038994",
     * title: "【券后价99元】爱奇艺vip会员12个月官方直充视频会员不支持TV端",
     * dtitle: "【爱奇艺】vip会员12个月包年",
     * originalPrice: 199,
     * actualPrice: 99,
     * shopType: 1,
     * goldSellers: 0,
     * monthSales: 29420,
     * twoHoursSales: 549,
     * dailySales: 1276,
     * commissionType: 3,
     * desc: "爱奇艺黄金会员1年，官方旗舰店，秒冲秒到账 ~爱奇艺黄金会员1年，官方旗舰店，秒冲秒到账 ~爱奇艺黄金会员1年，官方旗舰店，秒冲秒到账 ~",
     * couponReceiveNum: 29000,
     * couponLink: "https://uland.taobao.com/quan/detail?sellerId=1901369634&activityId=91e22d83cf444f52a3804e417cadc0d2",
     * couponEndTime: "2019-11-11 23:59:59",
     * couponStartTime: "2019-11-07 00:00:00",
     * couponPrice: 100,
     * couponConditions: "199",
     * activityType: 1,
     * createTime: "2019-11-10 09:40:03",
     * mainPic: "https://img.alicdn.com/imgextra/i2/3220506194/O1CN01E0DG8Y1vctm41Vqb6_!!3220506194.jpg",
     * marketingMainPic: "https://sr.ffquan.cn/relate_pic/o_1dovcu3f3n18a4817u016av19e4k.jpg",
     * sellerId: "1901369634",
     * cid: 7,
     * discounts: 0.5,
     * commissionRate: 20,
     * couponTotalNum: 100000,
     * haitao: 0,
     * activityStartTime: "",
     * activityEndTime: "",
     * shopName: "爱奇艺旗舰店",
     * shopLevel: 16,
     * descScore: 4.9,
     * brand: 0,
     * brandId: 134562310,
     * brandName: "爱奇艺",
     * hotPush: 439,
     * teamName: "淘起来",
     * itemLink: "https://detail.tmall.com/item.htm?id=43708038994",
     * tchaoshi: 0,
     * detailPics: "",
     * dsrScore: 4.9,
     * dsrPercent: 4.9,
     * shipScore: 4.9,
     * shipPercent: 34.17,
     * serviceScore: 4.9,
     * servicePercent: 27.78,
     * subcid: [ ],
     * tbcid: 201159608
     * }*/
    public
    function sync_goods_from_dataoke(){
        $ret = DataokeService::get_goods_list();
        if(!$ret || $ret['code']!==0){
            return return_array(600, '获取数据异常', $ret);
        }
        $ok_count = 0;
        foreach($ret['data']['list'] as $item){
            $newdata = [
                'itemid'        => $item['goodsId'],
                'title'         => $item['title'],
                'short_name'    => $item['dtitle'],
                'itemurl'       => $item['itemLink'],
                'desc'          => $item['desc'],
                'price'         => $item['originalPrice'],  //原价
                'price_pg'      => null,  //拼购价(拼购商品才有)
                'price_after'   => $item['actualPrice'],  //券后价
                'discount'      => $item['couponPrice'],  //券金额
                'quota'         => $item['couponConditions'],  //满多少可用券
                'picurl'        => $item['mainPic'],  //图片地址
                'picurls'       => $item['marketingMainPic'],  //图片地址列表
                'sales'         => $item['monthSales'],  //  销量
                'start_time'    => $item['couponStartTime'], //推广开始时间
                'end_time'      => $item['couponEndTime'],  //推广结束时间
                'ispg'          => 0,
                'comments'      => 0,  //评论数量
                'goodcommentsshare' => $item['descScore'],  //商品评价分  大淘客.描述分
                'commissionshare'   => $item['commissionRate'],     // 佣金比例
                'commission'        => $item['commissionRate']/100*$item['actualPrice'],  // 预估佣金
                'shopname'          => $item['shopName'],  //店铺名称
                'shopid'            => $item['sellerId'], //店铺ID
                'has_mall_coupon'   => 0,  //是否有店铺券 0否1是
                'coupon_total'  => $item['couponTotalNum'], //优惠券总数量
                'coupon_remain' => $item['couponTotalNum']-$item['couponReceiveNum'], //优惠券剩余数量
                'avg_desc'      => $item['descScore'], //描述评分
                'avg_lgst'      => $item['serviceScore'], //物流评分
                'avg_serv'      => $item['shipScore'], //服务评分
                'desc_pct'      => $item['dsrPercent'], //描述分击败同类店铺百分比
                'lgst_pct'      => $item['servicePercent'], //物流分击败同类店铺百分比
                'serv_pct'      => $item['shipPercent'], //服务分击败同类店铺百分比
                'type'          => 1, //1淘宝 2京东  3拼多多
            ];

            if(self::create($newdata)){
                $ok_count++;
            };
        }
        return return_array(200, $ret['msg'], $ret['data']);
    }

    public
    function get_page(array $get_req_params, PageParam $page_param){
        $where[] = ['type', '=', 1];
        if(!empty($get_req_params['keyword'])){
            $where[] = ['title', 'LIKE', "%$get_req_params[keyword]%"];
        }
        $fields = ['id', 'title', 'picurl', 'sales', 'price_after priceAfter', 'discount', 'price', 'discount'];
        return return_array(2000, 'success', $this->_page($where, $page_param, $fields));
    }

    /**
     *
     * @param $ret
     */
    public
    function get_privilege_link(self $ret){
        $datao_ret = DataokeService::get_privilege_link($ret['itemid']);
        if($datao_ret['code']!==0){
            return return_array(6000, '调用大淘客接口异常。', $datao_ret);
        }
        $ret =[
            'couponClickUrl' => $datao_ret['data']['couponClickUrl'],
            'itemUrl'        => $datao_ret['data']['itemUrl'],
            'tpwd'           => $datao_ret['data']['tpwd'],
            'shortUrl'       => $datao_ret['data']['shortUrl'],
        ];
        return return_array(2000, 'success', $ret);

    }
}
